package com.hutu.core.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl implements IRedisService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @param key     key
     * @param seconds
     * @return
     * @Description: 获取自增长值 (每调用1次，ID自增1)
     */
    public  Long getIncr(String key, int seconds) {
        RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        Long increment = entityIdCounter.getAndIncrement();
        if(increment.equals(0L)){
            increment = 1L; //序列从1开始
            entityIdCounter.getAndIncrement();
        }
        entityIdCounter.expire(seconds, TimeUnit.SECONDS);
        return increment;
    }

    /**
     * @Description: 初始化自增长值
     * @param key key
     * @param value 当前值
     */
    public void setIncr(String key, Long value) {
        RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        counter.set(value);
        setValue(key,counter.longValue());
        //counter.expire(0, TimeUnit.SECONDS);
    }

    @Override
    public void setValue(String key, Map<String, Object> value) {
        ValueOperations<String, Object> vo = redisTemplate.opsForValue();
        vo.set(key, value);
        redisTemplate.expire(key, 1, TimeUnit.HOURS); // 这里指的是1小时后失效
    }

    @Override
    public Object getValue(String key) {
        ValueOperations<String, String> vo = redisTemplate.opsForValue();
        return vo.get(key);
    }

    @Override
    public  <T> T getCache(String key, Class<T> clazz) {
        Object value = getValue(key);
        if (value == null) {
            return null;
        }

        if (clazz == String.class) {
            return (T) value;
        } else if (clazz == Integer.class) {
            return (T) Integer.valueOf(value.toString());
        } else if (clazz == Long.class) {
            return (T) Long.valueOf(value.toString());
        } else if (clazz == Double.class) {
            return (T) Double.valueOf(value.toString());
        } else if (clazz == Float.class) {
            return (T) Float.valueOf(value.toString());
        } else {
            throw new IllegalArgumentException("Unsupported data type: " + clazz.getName());
        }
    }

    @Override
    public void incr(String cacheKey, int seconds) {
        Long cache = getCache(cacheKey, Long.class);
        cache++;
        setIncr(cacheKey,cache);
    }


    @Override
    public void setValue(String key, String value) {
        ValueOperations<String, Object> vo = redisTemplate.opsForValue();
        vo.set(key, value);
        redisTemplate.expire(key, 1, TimeUnit.HOURS); // 这里指的是1小时后失效
    }

    //TTL默认是-1，但是如果设置了expire还是没用，会过期的
    @Override
    public void setValue(String key, Object value) {
        ValueOperations<String, Object> vo = redisTemplate.opsForValue();
        vo.set(key, value);
        redisTemplate.expire(key, 24, TimeUnit.HOURS); // 这里指的是24小时后失效
    }

    @Override
    public Object getMapValue(String key) {
        ValueOperations<String, String> vo = redisTemplate.opsForValue();
        return vo.get(key);
    }


}

